namespace SienSchoofsProject.Migrations
{
    using SienSchoofsProject.Models;
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using System.Web.Security;
    using WebMatrix.WebData;

    internal sealed class Configuration : DbMigrationsConfiguration<SienSchoofsProject.Models.FilmDb>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(SienSchoofsProject.Models.FilmDb context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            context.Films.AddOrUpdate(r => r.Name, //kijk of een van deze restaurants bestaat volgens naam
                new Film { Name = "Ender's Game", Year = 2013, Director = "Gavin Hood" },
                new Film { Name = "The Broken Circle Breakdown", Year = 2012, Director = "Felix van Groeningen" },
                new Film
                {
                    Name = "Iron Man 3",
                    Year = 2013,
                    Director = "Shane Black",
                    Reviews =
                        new List<FilmReview> {
                        new FilmReview { Rating = 9, Body="Great!", ReviewerName="Sien"}
                        }
                });

            for (int i = 0; i < 1000; i++)
            {
                context.Films.AddOrUpdate(r => r.Name,
                    new Film { Name = i.ToString(), Year = 2014, Director = "Unknown" });
            }

            SeedMembership();

        }

        private void SeedMembership()
        {
            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

            var roles = (SimpleRoleProvider)Roles.Provider;
            var membership = (SimpleMembershipProvider)Membership.Provider;

            /*
             * login: Sientest
             * Wachtwoord: Sientest
             * rol: Admin
             */
            if (!roles.RoleExists("Admin"))
            {
                roles.CreateRole("Admin");
            }
            if (membership.GetUser("Sientest", false) == null)
            {
                //membership.CreateUserAndAccount("Sientest", "Sientest");
                WebSecurity.CreateUserAndAccount("Sientest", "Sientest", new { emailadres = "sientjeschoofs@hotmail.com" }, false);
            }
            if (!roles.GetRolesForUser("Sientest").Contains("Admin"))
            {
                roles.AddUsersToRoles(new[] { "Sientest" }, new[] { "Admin" });
            }

           /*
            * login: Gebruiker1
            * Wachtwoord: Gebruiker1
            * rol: Standaard
            */
            if (!roles.RoleExists("Standaard"))
            {
                roles.CreateRole("Standaard");
            }
            if (membership.GetUser("Gebruiker1", false) == null)
            {
                //membership.CreateUserAndAccount("Gebruiker1", "Gebruiker1");
                WebSecurity.CreateUserAndAccount("Gebruiker1", "Gebruiker1", new { emailadres = "sien.schoofs@student.pxl.be" }, false);
            }
            if (!roles.GetRolesForUser("Gebruiker1").Contains("Standaard"))
            {
                roles.AddUsersToRoles(new[] { "Gebruiker1" }, new[] { "Standaard" });
            }
        }
    }
}
